package self.hoy.demo.cloud.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FlowLimitController {

    @GetMapping("/test-a")
    public String testA() {
        return "----- test a";
    }

    @GetMapping("/test-b")
    public String testB() {
        System.out.println("this is test b " + System.currentTimeMillis());
        return "----- test b";
    }

    @GetMapping("/test-c")
    @SentinelResource(value = "test-c-xxx", blockHandler = "testCBlockHandler")
    public String testC(@RequestParam(value = "p1", required = false) String p1,
                        @RequestParam(value = "p2", required = false) String p2) {
        return "this is test c " + System.currentTimeMillis();
    }
    public String testCBlockHandler(String p1, String p2, BlockException be){
        return "Block Exception of test c " + System.currentTimeMillis();
    }

    @GetMapping("/test-d")
    public String testD() {
        System.out.println("into test d " + System.currentTimeMillis());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            return "interrupt in test d";
        }
        return "----- test d";
    }
}
